/*
*   Coder : Dzlua
*   Email : 505544956@qq.com
*   Time  : 2017/08/08
*/
#pragma once

#include <string>

class DataPkg {
public:
    explicit DataPkg(size_t max_size) : size_(0) {
        data_ = new char[max_size + 1];
    }

    ~DataPkg() {
        delete[] data_;
    }
public:
    const void* data() {
        return data_;
    }
    
    size_t size() {
        return size_;
    }

    const char* cstring() {
        memset(data_ + size_, 0, 1);
        return (const char*)data_;
    }

    std::string string() {
        return std::move(std::string(data_, size_));
    }
    
    void add(const char ch) {
        memcpy(data_ + size_, &ch, 1);
        ++size_;
    }
    
    void add(const std::string &str) {
        this->add(str.data(), str.length());
    }
    
    void add(const void *data, size_t size) {
        memcpy(data_ + size_, data, size);
        size_ += size;
    }
private:
    char* data_;
    size_t size_;
};